import { Column, Entity, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Exclude } from 'class-transformer';
import { GenderType, StatusValue } from '@/enum/common.enum';
import { ApiProperty } from '@nestjs/swagger';
import { $enum } from 'ts-enum-util';
import { UserRole } from '@/modules/system/user-role/entities/user-role.entity';
import { UserSubuser } from '@/modules/system/user-subuser/entities/user-subuser.entity';
import { BaseTimeEntity } from '@/common/entities/base-time.entity';

@Entity({name:"yh_sys_user"})
export class User extends BaseTimeEntity {
    @PrimaryGeneratedColumn()
    id: number;
    
    @ApiProperty({ type: String, description: "用户名" })
    @Column({ type: "varchar", nullable: false, unique: true, comment: "用户名" })
    username: string;
    
    @ApiProperty({ type: String, description: "用户登录密码" })
    @Column({ type: "varchar", nullable: false, comment: "用户登录密码" })
    @Exclude({ toPlainOnly: true }) // 输出屏蔽盐
    password: string;
    
    @ApiProperty({ type: String, enum: $enum(GenderType).getValues(), description: "性别：1-男 2-女" })
    @Column({ type: "tinyint", default: GenderType.WOMAN, comment: "性别：1-男 2-女" })
    gender: number;
    
    @ApiProperty({ type: String, description: "用户手机号码" })
    @Column({ type: "varchar", length: 20, default: "", comment: "用户手机号码" })
    photo: string;
    
    @ApiProperty({ type: String, description: "用户地址" })
    @Column({ type: "varchar", default: "", comment: "用户地址" })
    address: string;
    
    @ApiProperty({ type: Number, description: "账户余额" })
    @Column({ type: "decimal", default: 0, comment: "账户余额" })
    balance: number;
    
    @ApiProperty({ type: String, description: "头像url" })
    @Column({ type: "varchar", default: "", comment: "头像地址" })
    public avatar: string;
    
    @ApiProperty({ type: String, enum: $enum(StatusValue).getValues(), description: "状态：0-禁用 1-启用" })
    @Column({ type: "tinyint", default: StatusValue.NORMAL, comment: "状态：0-禁用 1-启用" })
    status: number;
    
    // @ManyToMany(() => Role, (roles) => roles.users)
    // @JoinTable({
    // 	// name: "user_role", // 指定中间表的名称
    // 	// joinColumn: { name: "userId" }, // 指定User实体类在中间表中的列名
    // 	// inverseJoinColumn: { name: "roleId" } // 指定Role实体类在中间表中的列名
    // })
    // roles: Role[];
    @OneToMany(() => UserRole, (userRole) => userRole.user)
    userRole: UserRole[];
    
    //设置为userSubuser.subuser 默认name为id关联referencedColumnName为subuserId不需要指定
    @OneToOne(() => UserSubuser, (userSubuser) => userSubuser.subuser)
        // @JoinColumn({ name: "id", referencedColumnName: "subuserId" })
    parentUser: UserSubuser;
    
    @OneToMany(() => UserSubuser, (userSubuser) => userSubuser.user)//默认name为id不需要指定
        // @JoinColumn({ name: "id", referencedColumnName: "subuserId" })
    childUser: UserSubuser[];
    // @OneToMany(() => Logs, (logs) => logs.user, { cascade: true })
    // logs: Logs[];
    
}
